package org.advanced.collection;

import java.util.Iterator;
import java.util.List;

/**
 * @author John.zhang
 * @version Id: ThreadIterator, v 1.0 2020/12/2 14:45 ZhiYong Exp $
 */
public class ThreadIterator extends Thread {

    private List list;

    public ThreadIterator(List list) {
        this.list = list;
    }

    @Override
    public void run() {
        while (true){
            for (Iterator iterator = list.iterator();iterator.hasNext();){
                //modCount != expectedModCount ConcurrentModificationException
                //包含快速失败机制，应对并发集合迭代过程中内部结构变化的一种防护措施
                iterator.next();
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
